package typage;
import java.util.*;

// tables de hachage telles qu'introduites en cours
// i.e. on peut avoir plusieurs associations pour une meme cle
// et remove supprime la derniere uniquement

public class Table {
    class Bucket {
	Object key;
	Object value;
	Bucket next;
	Bucket(Object k, Object v, Bucket n) { key = k; value = v; next = n; }
	Object find(Object k) {
	    if (k.equals(key)) return value;
	    return next == null ? null : next.find(k);
	}
	boolean containsKey(Object k) {
	    if (k.equals(key)) return true;
	    return next != null && next.containsKey(k);
	}
	Bucket remove(Object k) {
	    if (k.equals(key)) return next;
	    Bucket n = next == null ? null : next.remove(k);
	    return new Bucket(key, value, n);
	}
    }
    static final int M = 97;
    Bucket[] table = new Bucket[M];
    void put(Object k, Object v) {
	int i = Math.abs(k.hashCode()) % M;
	assert 0 <= i && i < M;
	table[i] = new Bucket(k, v, table[i]);
    }
    Object get(Object k) {
	int i = Math.abs(k.hashCode()) % M;
	assert 0 <= i && i < M;
	return table[i]==null ? null : table[i].find(k);
    }
    boolean containsKey(Object k) {
	int i = Math.abs(k.hashCode()) % M;
	assert 0 <= i && i < M;
	return table[i]!=null && table[i].containsKey(k);
    }
    void remove(Object k) {
	int i = Math.abs(k.hashCode()) % M;
	assert 0 <= i && i < M;
	table[i] = table[i] == null ? null : table[i].remove(k);
    }
}

